package com.example.listener;

import com.example.util.ShardHashUtils;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ReceiveAndSendMsg {

    private static final String queueNamePrefix="test";

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RabbitListener(queuesToDeclare = @Queue(value = "up_queue"))
    public void receiveUp(String msg, Channel channel, Message message) throws IOException {
        int shardNo = ShardHashUtils.getShardNo(msg, 2);
        String shardQueueName = queueNamePrefix + "_" + shardNo;
        rabbitTemplate.convertAndSend(shardQueueName, msg+"do something");
    }
}
